/**
 * Bakalarska prace - Inteligentni budik
 * @package sleepCatcher
 * @file FormAlarms.h
 * @author Jan Pesava - xpesav00
 * @email xpesav00@stud.fit.vutbr.cz
 * @date 24. 12. 2011
 */

#include "SCSettings.h"
#include "SCDatabase.h"
#include "models/SCHelpers.h"

Integer SCSettings::windowSize = 0; //at minute
Integer SCSettings::partLength = 0; //at minute
Integer SCSettings::awakeTimeDifference = 0; // at minute
Integer SCSettings::language = 0;
Integer SCSettings::sleepLength = 0; // at minute;
Integer SCSettings::compression = 0;
Integer SCSettings::sleepPeriod = 0; // at minute;


SCSettings::SCSettings()
{
	// TODO Auto-generated constructor stub
}

SCSettings::~SCSettings()
{
	// TODO Auto-generated destructor stub
}

result
SCSettings::Construct()
{
	AppLog("Settings initializing.");

	// Set default Values
	SetDefaultValue();

	// Load xml data
	Load();

	return E_SUCCESS;
}

result
SCSettings::Destruct()
{
	AppLog("Settings destructed.");
	return E_SUCCESS;
}

void
SCSettings::SetWindowSize(int minute)
{
	windowSize = minute;
}

int
SCSettings::GetWindowSize()
{
	return windowSize.ToInt();
}

void
SCSettings::SetPartLength(int minute)
{
	partLength = minute;
}

int
SCSettings::GetPartLength()
{
	return partLength.ToInt();
}

void
SCSettings::SetAwakeTimeDifference(int minute)
{
	awakeTimeDifference = minute;
}

int
SCSettings::GetAwakeTimeDifference()
{
	return awakeTimeDifference.ToInt();
}

void
SCSettings::SetLanguage(int value)
{
	language = value;
}

int
SCSettings::GetLanguage()
{
	return language.ToInt();
}

void
SCSettings::SetSleepLength(int value)
{
	sleepLength = value;
}

int
SCSettings::GetSleepLength()
{
	return sleepLength.ToInt();
}

void
SCSettings::SetCompression(int value)
{
	compression = value;
}

int
SCSettings::GetCompression()
{
	return compression.ToInt();
}

void
SCSettings::SetSleepPeriod(int value)
{
	sleepPeriod = value;
}

int
SCSettings::GetSleepPeriod()
{
	return sleepPeriod.ToInt();
}

int
SCSettings::ConvertMinuteToTicks(int minute)
{
	return TimeSpan::NUM_OF_TICKS_IN_MINUTE * minute;
}

int
SCSettings::GetWindowSizeTicks()
{
	return ConvertMinuteToTicks(windowSize.ToInt());
}

int
SCSettings::GetPartLengthTicks()
{
	return ConvertMinuteToTicks(partLength.ToInt());
}

int
SCSettings::GetAwakeTimeDifferenceTicks()
{
	return ConvertMinuteToTicks(awakeTimeDifference.ToInt());
}

int
SCSettings::GetSleepPeriodTicks()
{
	return ConvertMinuteToTicks(sleepPeriod.ToInt());
}

result
SCSettings::Load()
{
	xmlTextReaderPtr xmlReader;
	int xmlNodeType, depth, maxDepth = 4;
	String namesPath[maxDepth];
	String name, value;

	xmlReader = xmlReaderForFile("/Home/Settings/config.xml", NULL, 0);

	if(xmlReader != NULL)
	{
		while(xmlTextReaderRead(xmlReader) == 1)
		{
			xmlNodeType = xmlTextReaderNodeType(xmlReader);
			depth = xmlTextReaderDepth(xmlReader);

			if(xmlNodeType == XML_ELEMENT_NODE)
			{
				Osp::Base::Utility::StringUtil::Utf8ToString ((char*)xmlTextReaderConstName(xmlReader), name);
				namesPath[depth] = name;
			}

			if(xmlNodeType == XML_TEXT_NODE)
			{
				Osp::Base::Utility::StringUtil::Utf8ToString ((char*)xmlTextReaderConstValue(xmlReader), value);
				SetAttribute(namesPath, value, depth);
			}

			if(xmlNodeType == XML_DTD_NODE)
			{
				//AppLog("XML_DTD_NODE");
			}

			if(xmlNodeType == XML_ELEMENT_DECL)
			{
				//AppLog("XML_ELEMENT_DECL");
			}
		}

		xmlFreeTextReader(xmlReader);

	} else {
		AppLog("Xml reader nebyl vytvoren.");
		return E_FAILURE;
	}

	return E_SUCCESS;
}

result
SCSettings::SetAttribute(String namePath[], String valueStr, int depth)
{
	int attributesIndex = depth-2;
	int nameIndex = depth-1;
	int valueInt = 0;

	// WindowsSize
	if(namePath[attributesIndex] == "window_size")
	{
		if(namePath[nameIndex] == "minute")
		{
			Integer::Parse(valueStr, valueInt);
			SCSettings::SetWindowSize(valueInt);
		}
	}

	// AwakeTimeDifference
	if(namePath[attributesIndex] == "awake_time_difference")
	{
		if(namePath[nameIndex] == "minute")
		{
			Integer::Parse(valueStr, valueInt);
			SCSettings::SetAwakeTimeDifference(valueInt);
		}
	}

	// PartLength
	if(namePath[attributesIndex] == "part_length")
	{
		if(namePath[nameIndex] == "minute")
		{
			Integer::Parse(valueStr, valueInt);
			SCSettings::SetPartLength(valueInt);
		}
	}

	// SleepLength
	if(namePath[attributesIndex] == "sleep_length")
	{
		if(namePath[nameIndex] == "value")
		{
			Integer::Parse(valueStr, valueInt);
			SCSettings::SetSleepLength(valueInt);
		}
	}

	// Language
	if(namePath[attributesIndex] == "language")
	{
		if(namePath[nameIndex] == "value")
		{
			Integer::Parse(valueStr, valueInt);
			SCSettings::SetLanguage(valueInt);
		}
	}

	// Compression
	if(namePath[attributesIndex] == "compression")
	{
		if(namePath[nameIndex] == "value")
		{
			Integer::Parse(valueStr, valueInt);
			SCSettings::SetCompression(valueInt);
		}
	}

	// SleepPeriod
	if(namePath[attributesIndex] == "sleep_period")
	{
		if(namePath[nameIndex] == "value")
		{
			Integer::Parse(valueStr, valueInt);
			SCSettings::SetSleepPeriod(valueInt);
		}
	}


	return E_SUCCESS;
}

result
SCSettings::Save()
{
	xmlTextWriterPtr xmlWriter;

	xmlWriter = xmlNewTextWriterFilename("/Home/Settings/config.xml", 0);

	if(xmlWriter != NULL)
	{
		xmlTextWriterStartDocument(xmlWriter, NULL, "ISO-8859-1", NULL);
		xmlTextWriterStartElement(xmlWriter, (xmlChar*) "scsettings");

		//window_size
		string *pWindowSizeStr = SCHelpers::GetCString(windowSize.ToString());
		xmlTextWriterStartElement(xmlWriter, (xmlChar*) "window_size");
		xmlTextWriterWriteElement(xmlWriter, (xmlChar*) "minute", (xmlChar*) pWindowSizeStr->c_str());
		xmlTextWriterEndElement(xmlWriter);

		//part_length
		string *pPartLengthStr = SCHelpers::GetCString(partLength.ToString());
		xmlTextWriterStartElement(xmlWriter, (xmlChar*) "part_length");
		xmlTextWriterWriteElement(xmlWriter, (xmlChar*) "minute", (xmlChar*) pPartLengthStr->c_str());
		xmlTextWriterEndElement(xmlWriter);

		//awake_time_difference
		string *pAwakeTimeDiffStr = SCHelpers::GetCString(awakeTimeDifference.ToString());
		xmlTextWriterStartElement(xmlWriter, (xmlChar*) "awake_time_difference");
		xmlTextWriterWriteElement(xmlWriter, (xmlChar*) "minute", (xmlChar*) pAwakeTimeDiffStr->c_str());
		xmlTextWriterEndElement(xmlWriter);

		//awake_time_difference
		string *pSleepLengthStr = SCHelpers::GetCString(sleepLength.ToString());
		xmlTextWriterStartElement(xmlWriter, (xmlChar*) "sleep_length");
		xmlTextWriterWriteElement(xmlWriter, (xmlChar*) "value", (xmlChar*) pSleepLengthStr->c_str());
		xmlTextWriterEndElement(xmlWriter);

		//awake_time_difference
		string *pLanguageStr = SCHelpers::GetCString(language.ToString());
		xmlTextWriterStartElement(xmlWriter, (xmlChar*) "language");
		xmlTextWriterWriteElement(xmlWriter, (xmlChar*) "value", (xmlChar*) pLanguageStr->c_str());
		xmlTextWriterEndElement(xmlWriter);

		//compression
		string *pCompressionStr = SCHelpers::GetCString(compression.ToString());
		xmlTextWriterStartElement(xmlWriter, (xmlChar*) "compression");
		xmlTextWriterWriteElement(xmlWriter, (xmlChar*) "value", (xmlChar*) pCompressionStr->c_str());
		xmlTextWriterEndElement(xmlWriter);

		//compression
		string *pSleepPeriodStr = SCHelpers::GetCString(sleepPeriod.ToString());
		xmlTextWriterStartElement(xmlWriter, (xmlChar*) "sleep_period");
		xmlTextWriterWriteElement(xmlWriter, (xmlChar*) "value", (xmlChar*) pSleepPeriodStr->c_str());
		xmlTextWriterEndElement(xmlWriter);

		xmlTextWriterEndElement(xmlWriter);
		xmlFreeTextWriter(xmlWriter);

		//dealloc
		delete pLanguageStr;
		delete pPartLengthStr;
		delete pSleepLengthStr;
		delete pSleepPeriodStr;
		delete pWindowSizeStr;
		delete pCompressionStr;
		delete pAwakeTimeDiffStr;


	} else	{
		AppLog("Bad xmlWriter");
		return E_FAILURE;
	}

	return E_SUCCESS;
}

void
SCSettings::SetDefaultValue()
{
	windowSize = 5; //at minute
	partLength = 30; //at minute
	awakeTimeDifference = 30; // at minute
	sleepLength = 540;
	language = LANGUAGE_EN;
	compression = 100;
	sleepPeriod = 100; // at minute
}

String
SCSettings::GetLanguageString(int language)
{
	if(language == LANGUAGE_EN)
	{
		return String("English");
	} else if(language == LANGUAGE_CS)
	{
		return String("Čeština");
	} else if(language == LANGUAGE_DE)
	{
		return String("Deutsch");
	} else {
		return String("Unknown");
	}
}

